package com.yanqu.road.dao.impl.activity.chinesechess;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.chinesechess.ChineseChessConfig;
import com.yanqu.road.entity.activity.chinesechess.ChineseChessCreateConfig;
import com.yanqu.road.entity.activity.chinesechess.ChineseChessMoveConfig;
import com.yanqu.road.entity.activity.chinesechess.ChineseChessWeightConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChineseChessConfigDaoImpl extends TempDao {

    public static String getChessCommentSql = "select * from t_s_activity_chess where activityId= ?";
    public Map<Integer, ChineseChessConfig> getChessMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ChineseChessConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getChessCommentSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChineseChessConfig data = new ChineseChessConfig();
                data.setId(rs.getInt("id"));
                data.setScore(rs.getInt("score"));
                data.setChessSkinId(rs.getString("chessSkinId"));
                data.setCanGo(rs.getString("canGo"));
                data.setMoveLimit(rs.getString("moveLimit"));
                data.setMoveText(rs.getString("moveText"));
                data.setEatText(rs.getString("eatText"));
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String getCreateCommentSql = "select * from t_s_activity_chesscreate where activityId= ?";
    public Map<Integer, ChineseChessCreateConfig> getChessCreateMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ChineseChessCreateConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getCreateCommentSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChineseChessCreateConfig data = new ChineseChessCreateConfig();
                data.setId(rs.getInt("id"));
                data.setChessNumLimit(rs.getInt("chessNumLimit"));
                data.setStep(rs.getInt("step"));
                data.setCreateNum(rs.getInt("createNum"));
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String getWeightCommentSql = "select * from t_s_activity_chessweight where activityId= ?";
    public Map<Integer, ChineseChessWeightConfig> getChessWeightMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ChineseChessWeightConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getWeightCommentSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChineseChessWeightConfig data = new ChineseChessWeightConfig();
                data.setId(rs.getInt("id"));
                data.setScoreLimit(rs.getInt("scoreLimit"));
                data.setWeight(rs.getString("weight"));
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }
    public static String getMoveCommentSql = "select * from t_s_activity_chessmove where activityId= ?";
    public Map<Integer, ChineseChessMoveConfig> getChessMoveMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ChineseChessMoveConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getMoveCommentSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChineseChessMoveConfig data = new ChineseChessMoveConfig();
                data.setId(rs.getInt("id"));
                data.setRounds(rs.getInt("rounds"));
                data.setChess(rs.getString("chess"));
                data.setChessList(StringUtils.stringToIntegerList(data.getChess(),";"));
                data.setAccessExpected(rs.getInt("accessExpected"));
                data.setCouplingExpected(rs.getInt("couplingExpected"));
                result.put(data.getRounds(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }
}
